package org.yi.fc.controller.admin.column;

import java.util.Calendar;

import org.apache.commons.lang.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.yi.fc.controller.base.BaseController;
import org.yi.fc.entity.FCategory;

public class CategoryController extends BaseController {
	
	private static final Logger logger = LoggerFactory.getLogger(CategoryController.class);
	
	/**
	 * 添加栏目 
	 */
	public void add(){
		setAttr("cats", FCategory.dao.find("select * from f_category where pid is null or pid = ''"));
		render("add.html");
	}
	
	public void index(){
		setAttr("list", FCategory.dao.getAll());
		render("index.html");
	}
	
	public void save(){
		
		if(isPostMethod()){
			try{
				new FCategory()
					.set("name", getPara("name"))
					.set("alias", getPara("alias"))
					.set("pid", StringUtils.isBlank(getPara("pid")) ? null : getPara("pid"))
					.set("url", getPara("url"))
					.set("create_time", Calendar.getInstance().getTime())
					.set("create_user", getCurrentUser().getLong("id"))
					.save();
			}catch(Exception e){
				logger.error("保存栏目失败", e);
				addError(e.getMessage());
			}
		}else{
			String msg = "数据来源错误！";
			addError(msg);
			logger.warn(msg);
		}
		redirect("/admin/category");
	}
	
}
